#pragma once
#include "../../message/interface/ClientMessageInterface.hpp"
#include "../../type/ClientType.hpp"
#include "ClientIOCPExecution.hpp"
#include "../interface/ClientExecutionInterface.hpp"
#include "../../tool/MessagePoolsTool.hpp"



/**
 * 处理消息的执行器
 */
class ClientMessageExecution : public ClientExecutionInterface{

private:

    class MessageHandle {
    private:
        std::queue<ClientMessageInterface*> m_messageQueue;
        std::mutex m_mutex;

    private:




    };
private:

    static ClientType::EXECUTION_TYPE executionType;

    ClientIOCPExecution* m_IOCPExecution;

    ClassReflectionTool::ClassFactory<ClientType::MESSAGE_TYPE>* m_messagesFactory;

    MessagePoolsTool<ClientType::MESSAGE_TYPE> m_messagesPool;


private:
    /**
     * 分配接收的消息到队列
     */
    void allocateReceviceMessageToQueue() {
        while (true) {
            std::string messagestr = this->m_IOCPExecution->getReceviceMessage();
            ClientMessageInterface* message = nullptr;

            //从消息池中获取消息
            if (!this->m_messagesPool.acquireMessge(messagestr, message)) continue;
        }

    }


    /**
     *
     * @return
     */
    bool startCallBackFunction() override {

        //登记IOCP执行器
        this->m_IOCPExecution = static_cast<ClientIOCPExecution*>(
            static_cast<ClientMainExecution*>(this->m_mainExecution)->
            getExecutionByExecutionType(ClientType::CLIENT_IOCP_EXECUTION));

        //初始化工厂单例
        this->m_messagesFactory = ClassReflectionTool::ClassFactory<ClientType::MESSAGE_TYPE>::getSingleton();

        return true;
    }


    /**
     *
     * @return
     */
    bool closeCallBackFunction() override {


        return true;
    }

public:

    ClientMessageExecution() {
        this->m_IOCPExecution = nullptr;
        this->m_messagesFactory = nullptr;
    }
    ~ClientMessageExecution(){}

    /**
     *
     * @return
     */
    ClientType::EXECUTION_TYPE getType() const override {
        return ClientMessageExecution::executionType;
    }
};

ClientType::EXECUTION_TYPE ClientMessageExecution::executionType = ClientType::EXECUTION_TYPE::CLIENT_MESSAGE_EXECUTION;